const Base = require("./base.js");
const path = require("path");
const { cp } = require("shelljs");
const puppeteer = require("puppeteer");
module.exports = class extends Base {
  async indexAction() {
    const baiduPush = this.service("baidu/push");
    await baiduPush.urls("/");
    return this.json({ code: 0 });
  }

  async areaAction() {
    return this.json({
      errno: 0,
      data: require("../../common/lib/area_json")
    });
  }

  async uploadAction() {
    const file = this.file("file");
    const get = this.get();
    // 如果上传的是 png 格式的图片文件，则移动到其他目录
    const types = ["image/png", "image/jpeg"];
    if (file && types.includes(file.type)) {
      const savepath = `/upload/${think.datetime(
        Date.now(),
        "YYYY-MM-DD"
      )}/${think.uuid()}${path.extname(file.name)}`;
      const filepath = path.join(think.ROOT_PATH, `www/${savepath}`);
      think.mkdir(path.dirname(filepath));
      await cp("-rf", file.path, filepath);
      let data = {
        name: file.name,
        type: file.type,
        url: savepath
      };
      if (get && get.comefrom === "wangeditor") {
        data = [savepath];
      }
      return this.json(
        this.format({
          errno: 0,
          data
        })
      );
    }
    return this.json(
      this.format({
        errno: -1,
        data: file
      })
    );
  }

  /**
   * cli
   * @returns {Promise<any>}
   */
  async puppeteerAction() {
    const browser = await puppeteer.launch({
      headless: false
    });
    const page = await browser.newPage();
    page.setViewport({
      width: 1366,
      height: 768
    });
    await page.goto("https://www.haodf.com/jibing/xiaoerke/list.htm");
    const handlerName = "HaodfJibing";
    const Handler = require("../../common/lib/puppeteer/" + handlerName);
    const hanlder = new Handler(page);
    if (hanlder.pre) await hanlder.pre();
    const data = await hanlder.evaluate();
    if (hanlder.post) await hanlder.post(data);
    await browser.close();
    return this.json({ errno: 0 });
  }

  async puppeteercliAction() {
    const db = this.model("puppeteer");
    let tasks = [1];
    let pageNum = 0;
    while (tasks.length) {
      const task = tasks[0];
      pageNum++;
      if (task && task.id) {
        const browser = await puppeteer.launch({
          headless: true
        });
        const page = await browser.newPage();
        await page.goto(task.url);
        const handlerName = task.module;
        const Handler = require("../../common/lib/puppeteer/" + handlerName);
        const hanlder = new Handler(page);
        if (hanlder.pre) await hanlder.pre();
        const data = await hanlder.evaluate();
        if (hanlder.post) await hanlder.post(data);
        await browser.close();
      }
      tasks = await db.limit(pageNum - 1, 1).select();
    }
  }

  async catecliAction() {
    let url = "https://www.haodf.com/jibing/xiaoerke/list.htm";
    const browser = await puppeteer.launch({
      headless: true
    });
    const page = await browser.newPage();
    await page.goto(url);
    const Hanlder = require("../../common/lib/puppeteer/HaodfJibing");
    const handler = new Hanlder(page);
    const data = await handler.evaluate();
    if (handler.post) await handler.post(data);
  }
};
